package com.Jano.servlet;

import com.Jano.dto.UserDto;
import com.Jano.entity.Question;
import com.Jano.entity.Questionnaire;
import com.Jano.service.ChoiceService;
import com.Jano.service.Impl.ChoiceServiceImpl;
import com.Jano.service.Impl.QuestionServiceImpl;
import com.Jano.service.Impl.QuestionnaireServiceImpl;
import com.Jano.service.QuestionService;
import com.Jano.service.QuestionnaireService;
import com.Jano.vo.SurveyForm;
import com.Jano.vo.option;
import com.Jano.vo.question;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

import static com.Jano.constant.BusinessConstant.SESSION_USER;

/**
 * description: QuestionnaireServlet <br>
 * date: 2022/6/6 7:45 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

@WebServlet(urlPatterns = "/questionnaire/*")
public class QuestionnaireServlet extends AbstractBaseServlet {
    QuestionnaireService questionnaireService = new QuestionnaireServiceImpl();
    QuestionService questionService = new QuestionServiceImpl();
    ChoiceService choiceService = new ChoiceServiceImpl();

    @Override
    public Object doService(String action, HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
        if ("create".equals(action)) {
            try {
                InputStream inputStream = req.getInputStream();
                List<String> lines= IOUtils.readLines(inputStream);
                String json =lines.stream().collect(Collectors.joining(""));
                SurveyForm form = JSON.parseObject(json, SurveyForm.class);
                Object sessionUserObj = req.getSession().getAttribute(SESSION_USER);
                UserDto userDto =(UserDto) sessionUserObj;
                System.out.println(userDto.getUserId());
                System.out.println(form);
                System.out.println(json);
                synchronized (new Object()){
                    //存入数据库
                    questionnaireService.createQuestionnaire(userDto.getUserId(),form.getTitle());
                    //读取数据库中的创建好的问卷对象
                    Questionnaire questionnaire = questionnaireService.selectQuestionnaireByTitle(form.getTitle());
                    //从前端读取所有创建的问题
                    List<question> questions = form.getQuestion();
                    for (question q:questions){
                        //得到当前的问题内容并创建
                        questionService.createQuestion(questionnaire.getQuestionnaireId(),q.getQtitle());
                        //查处当前问题的在数据库中的id
                        Question question = questionService.getQuestionByQuestionnaireIdAndContent(questionnaire.getQuestionnaireId(), q.getQtitle());
                        //得到当前问题的所有选项
                        List<option> options = q.getOptions();
                        for (int i = 0; i < options.size(); i++) {
                            choiceService.createChoice(question.getQuestionId(),options.get(i).getValue(), String.valueOf(i));
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if ("isExist".equals(action)){
            String title =req.getParameter("title");
            System.out.println(title);
            questionnaireService.isExistQuestionnaire(title);
        }
        return null;
    }
}
